package ${package.Controller};

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liqi.frame.comm.dto.HttpResult;
import com.liqi.frame.comm.dto.LayuiTableData;
import com.liqi.frame.comm.mybatisplus.Assist;
import ${package.Entity}.${entity};
import com.liqi.frame.comm.enums.ResultEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import ${package.Service}.${table.serviceName};
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@RestController
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}/ajax<#else>${table.entityPath}/ajax</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.entityName}AjaxController {
</#if>
    @Autowired
    private ${table.serviceName} ${table.entityPath}Service;

    @ApiOperation(value="获得 ${table.comment} 列表", notes = "通过查询参数获得 ${table.comment} 列表", httpMethod = "GET")
    @GetMapping({"getList"})
    public LayuiTableData getList(@RequestParam(required=true,defaultValue="1") Integer page,
                                  @RequestParam(required=false,defaultValue="10") Integer limit,
                                  ${entity} ${table.entityPath}) {
        Assist assist = Assist.getInstance();
        assist.setRequires(${table.entityPath}, Assist.AND_LIKE);
        PageHelper.startPage(page, limit);
        List list = ${table.entityPath}Service.selectList_commom(assist);
        PageInfo pageInfo = new PageInfo(list);
        return LayuiTableData.success(pageInfo.getList(), pageInfo.getTotal());
    }

    @ApiOperation(value="获得 ${table.comment} 对象", notes = "通过 ${table.comment} id获得 ${table.comment} 对象", httpMethod = "GET")
    @GetMapping({"getById"})
    public HttpResult getById(@RequestParam(required = true) String id) {
        ${entity} ${table.entityPath} = ${table.entityPath}Service.selectById_common(id);
        return HttpResult.success(${table.entityPath});
    }

    @ApiOperation(value="保存或修改 ${table.comment} 对象", notes = "保存或修改 ${table.comment} 对象", httpMethod = "POST")
    @PostMapping({"save"})
    public HttpResult save(${entity} ${table.entityPath}){
        int resultStatus = 0;
        if (${table.entityPath}.getId()==null || "".equals(${table.entityPath}.getId())) {
            resultStatus = ${table.entityPath}Service.insertObject_common(${table.entityPath});
        } else {
            resultStatus = ${table.entityPath}Service.updateObject_common(${table.entityPath});
        }
        if (resultStatus>0) {
            return HttpResult.success("保存成功！");
        } else {
            return HttpResult.failure("保存失败！");
        }
    }

    @ApiOperation(value="删除 ${table.comment} 对象", notes = "通过id删除 ${table.comment} 对象", httpMethod = "POST")
    @PostMapping({"delete"})
    public HttpResult delete(@RequestParam(required = true) String id) {
        int resultStatus = ${table.entityPath}Service.deleteById_common(id);
        if (resultStatus > 0) {
            return HttpResult.success();
        } else {
            return HttpResult.failure();
        }
    }
}
</#if>
